package com.itxx.homeWork.demo3;

import com.itxx.homeWork.domain.Student;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class ReflectionTest {
    //通过method.properties运行任意类的任意方法
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, IOException {
        Properties p = new Properties();
        p.load(new BufferedReader(new FileReader("D:\\develop\\workspace\\Advance_Code\\day16\\src\\com\\itxx\\homeWork\\method.properties")));

       /* InputStream is = ClassLoader.getSystemResourceAsStream("method.properties");
        p.load(is);*/

        //创建Class对象
        Class aClass = Class.forName(p.getProperty("className"));
        //创建Constructor   无参构造方法对象
        Constructor constructor = aClass.getDeclaredConstructor();

        Constructor constructor1 = aClass.getDeclaredConstructor(String.class, Integer.class);

        //无参创建Stu对象
        Student stu1 = (Student) constructor.newInstance();
        Field name = aClass.getDeclaredField("name");
        name.setAccessible(true);
        name.set(stu1, "李四");
        Field age = aClass.getDeclaredField("age");
        age.setAccessible(true);
        age.set(stu1, 34);
        System.out.println(stu1);

        System.out.println("------");
        //有参创建Stu对象
        Student stu2 = (Student) constructor1.newInstance("张三", 12);
        System.out.println(stu2);
        System.out.println("------");

        Method method = aClass.getDeclaredMethod(p.getProperty("methodName"));
        Method method1 = aClass.getDeclaredMethod(p.getProperty("methodName"), Class.forName(p.getProperty("arg1")));
        method.setAccessible(true);
        method.invoke(stu1);
        method.invoke(stu2);
        method1.setAccessible(true);
        method1.invoke(stu1, 13);
        method1.invoke(stu2, 131);



    }

}
